﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ReallyReallyReallySimpleRoguelike.Screens
{
    abstract class Screen
    {

        public Screen()
        { 
            
        }

        public virtual void Show()
        {
            Console.Clear();
        }

        public void WriteString(string text, ConsoleColor color)
        {
            Console.ForegroundColor = color;
            List<string> lines;
            lines = new List<string>();

            string[] lineArray = text.Split("\r\n".ToArray(), StringSplitOptions.None);

     
            StringBuilder line = new StringBuilder();

            
            foreach (string arrayLine in lineArray)
            {
                string[] wordArray = arrayLine.Split(' ');
                foreach (string word in wordArray)
                {
                    Wrap(lines, line, word);
                }
                lines.Add(line.ToString());
                line.Clear();
            }

                StringBuilder output;
                output = new StringBuilder();
                foreach (string l in lines)
                {
                    output.Append(l);
                    output.Append("\r\n");
                }

                Console.Write(output.ToString());
            
        }
        private static void Wrap(List<string> lines, StringBuilder line, string word)
        {
            if (line.Length + word.Length >= Console.WindowWidth)
            {
                lines.Add(line.ToString());
                line.Clear();
                line.Append(word + " ");
            }
            else
                line.Append(word + " ");
        }
    }
}
